from flask import Blueprint, jsonify

from models import News

news_bp = Blueprint('news_pb', __name__)


@news_bp.route('')
def get_news_list():
    data = {
        'news': [],
        "meta": {
            "msg": "success",
            "status": 200
        }
    }
    news = News.query.all()
    news = [{
        "id":n.id,
        "title": n.title,
        "summary": n.summary,
        "img_url": n.img_url,
        "click": n.click,
        "fav": n.fav,
        "like": n.like,
        "view": n.view,
        "create_time": n.create_time  # .strftime('%Y-%m-%d %X')
    } for n in news]
    data["news"] = news
    return jsonify(data)


@news_bp.route('/<int:nid>')
def get_news(nid):
    data = {
        'news': {},
        "meta": {
            "msg": "success",
            "status": 200
        }
    }
    news = News.query.filter_by(id=nid).first()
    news = {
        "id": news.id,
        "title": news.title,
        "summary": news.summary,
        "img_url": news.img_url,
        "click": news.click,
        "fav": news.fav,
        "like": news.like,
        "view": news.view,
        "content": news.content,
        "create_time": news.create_time  # .strftime('%Y-%m-%d %X')
    }
    data["news"] = news
    return jsonify(data)
